package com.yxy.flybird;

import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.ParallaxBackground;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.ButtonSprite.OnClickListener;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.HorizontalAlign;

import android.util.Log;

public class SceneManager {

	private SimpleBaseGameActivity mContext;
	private ResourceManager mResourceManager;
	private ParallaxBackground mParallaxBackground;

	// add by jmc
	public ButtonSprite bs_simple, bs_general, bs_difficult, bs_metamorphosis;

	// text objects
	Text mScoreText_best;
	Text mScoreText;
	Text mGetReadyText;
	Sprite mInstructionsSprite;
	Text mCopyText;
	Text mYouSuckText;

	Bird mBird;

	// cloud
	Cloud mCloud;

	public SceneManager(SimpleBaseGameActivity context,
			ResourceManager resourceManager,
			ParallaxBackground parallaxBackground) {
		this.mContext = context;
		this.mResourceManager = resourceManager;
		this.mParallaxBackground = parallaxBackground;
	}

	public Scene createScene() {

		Scene mScene = new Scene();
		VertexBufferObjectManager vbo = mContext.getVertexBufferObjectManager();

		Sprite backgroundSprite = new Sprite(0, 0,
				mResourceManager.mBackgroundTextureRegion, vbo);
		mParallaxBackground.attachParallaxEntity(new ParallaxEntity(1,
				backgroundSprite));

		mScene.setBackground(mParallaxBackground);
		mScene.setBackgroundEnabled(true);

		// bird
		float birdStartXOffset = (MainActivity.CAMERA_WIDTH / 4)
				- (Bird.BIRD_WIDTH / 4);
		float birdYOffset = (MainActivity.CAMERA_HEIGHT / 2)
				- (Bird.BIRD_HEIGHT / 4);
		mBird = new Bird(birdStartXOffset, birdYOffset,
				mContext.getVertexBufferObjectManager(), mScene);

		// cloud
		float cloudStartXOffset = MainActivity.CAMERA_WIDTH;
		float cloudYOffset = (MainActivity.CAMERA_HEIGHT / 2)
				- (Cloud.CLOUD_HEIGHT / 2);
		mCloud = new Cloud(cloudStartXOffset, cloudYOffset,
				mContext.getVertexBufferObjectManager(), mScene);

		// score
		mScoreText_best = new Text(0, 24, mResourceManager.mScoreFont_best,
				"        ", new TextOptions(HorizontalAlign.CENTER),
				mContext.getVertexBufferObjectManager());
		mScoreText_best.setZIndex(3);
		mScene.attachChild(mScoreText_best);

		mScoreText = new Text(0, 96, mResourceManager.mScoreFont, "        ",
				new TextOptions(HorizontalAlign.CENTER),
				mContext.getVertexBufferObjectManager());
		mScoreText.setZIndex(3);
		mScene.attachChild(mScoreText);

		// get ready text
		mGetReadyText = new Text(0, 220, mResourceManager.mGetReadyFont,
				"Get Ready!", new TextOptions(HorizontalAlign.CENTER),
				mContext.getVertexBufferObjectManager());
		mGetReadyText.setZIndex(3);
		mScene.attachChild(mGetReadyText);
		centerText(mGetReadyText);

		// instructions image
		mInstructionsSprite = new Sprite(0, 0, 200, 172,
				mResourceManager.mInstructionsTexture,
				mContext.getVertexBufferObjectManager());
		mInstructionsSprite.setZIndex(3);
		mScene.attachChild(mInstructionsSprite);
		centerSprite(mInstructionsSprite);
		mInstructionsSprite.setY(mInstructionsSprite.getY() + 20);

		// copy text
		mCopyText = new Text(0, 750, mResourceManager.mCopyFont,
				"(c) JMC 2017", new TextOptions(HorizontalAlign.CENTER),
				mContext.getVertexBufferObjectManager());
		mCopyText.setZIndex(3);
		mScene.attachChild(mCopyText);
		centerText(mCopyText);

		// jmc button
		bs_simple = new ButtonSprite(6, 6, mResourceManager.simpleRegion, vbo);
		bs_simple.setZIndex(4);
		mScene.attachChild(bs_simple);
		bs_general = new ButtonSprite(MainActivity.CAMERA_WIDTH - 6
				- bs_simple.getWidth(), 6, mResourceManager.generalRegion, vbo);
		bs_general.setZIndex(4);
		mScene.attachChild(bs_general);
		bs_difficult = new ButtonSprite(6, MainActivity.CAMERA_HEIGHT / 2
				- bs_simple.getHeight() - 12, mResourceManager.difficultRegion,
				vbo);
		bs_difficult.setZIndex(4);
		mScene.attachChild(bs_difficult);
		bs_metamorphosis = new ButtonSprite(MainActivity.CAMERA_WIDTH - 6
				- bs_simple.getWidth(), MainActivity.CAMERA_HEIGHT / 2
				- bs_simple.getHeight() - 12,
				mResourceManager.metamorphosisRegion, vbo);
		bs_metamorphosis.setZIndex(4);
		mScene.attachChild(bs_metamorphosis);

		// you suck text
		mYouSuckText = new Text(0, MainActivity.CAMERA_HEIGHT / 2 - 100,
				mResourceManager.mYouSuckFont, "You Suck!", new TextOptions(
						HorizontalAlign.CENTER),
				mContext.getVertexBufferObjectManager());
		mYouSuckText.setZIndex(3);
		centerText(mYouSuckText);

		return mScene;
	}

	public static void centerSprite(Sprite sprite) {
		sprite.setX((MainActivity.CAMERA_WIDTH / 2) - (sprite.getWidth() / 2));
		sprite.setY((MainActivity.CAMERA_HEIGHT / 2) - (sprite.getHeight() / 2));
	}

	public void displayCurrentScore(int score) {
		mScoreText.setText("" + score);
		centerText(mScoreText);
	}

	public void displayBestScore(String msg) {
		Log.i("jmc", " msg = " + msg);
		mScoreText_best.setText("BEST");
		centerText(mScoreText_best);
		mScoreText.setText(msg);
		centerText(mScoreText);
	}

	private void centerText(Text text) {
		text.setX((MainActivity.CAMERA_WIDTH / 2) - (text.getWidth() / 2));
	}
}
